import Vue from 'vue'
import App from './App'
import store from './store'
// 全局引入uCharts图表
import * as config from './config'
import uView from '@/uni_modules/uview-ui'
import publicTools from './common/tool.js'
import domain from "./common/globalVariable.js"
import * as filters from './filters'
import UniDataSelect from './uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue';
import { uniIcons } from './uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
Object.keys(filters).forEach(key => {
	Vue.filter(key, filters[key])
})
Vue.prototype.publicTools = publicTools;// 挂载公共方法
Vue.prototype.domain = domain;
Vue.config.productionTip = false
// import uCharts from '@/uni_modules/qiun-data-charts'
Vue.use(uView)

Vue.component('uni-data-select', UniDataSelect);
Vue.component('uniIcons', UniDataSelect);

uni.$u.setConfig({
	// 修改$u.props对象的属性
	props: {
		// 修改radio组件的size参数的默认值，相当于执行 uni.$u.props.radio.size = 30
		radio: {
			size: 15
		},
		navbar: {
			bgColor: '#FFFFFF'
		}
	}
})
const baseUrl = config.def().baseUrl
Vue.config.productionTip = false

Vue.prototype.request = function(method, endpoint, token, data = {}) {
	let netWork = ''
	uni.getNetworkType({
		success: function(res) {
			netWork = res.networkType
		}
	})
	if (netWork === 'none') {
		uni.showToast({
			title: '当前无网络',
			icon: 'none',
			duration: 1500
		})
		return;
	}
	return new Promise(resolve => {
		let content = 'application/x-www-form-urlencoded; charset=UTF-8';
		if (method === 'post' || method === 'put' ||  method === 'delete') {
			content = 'application/json; charset=UTF-8';
		}
		uni.request({
			url: baseUrl + "/" + endpoint,
			method: method,
			data: data,
			header: {
				'Content-Type': content,
				'Authorization': 'Bearer ' + token
			},
			success: function(res) {
				if (res.data.code === 200) {
					resolve(res.data);
				} else if (res.data.code === 401) {
					let pages = getCurrentPages();
					let curPath = `/${pages[pages.length - 1].route}`; // 当前页
					let loginPath = '/pages/login/login'; // 登录页
					console.log('curPath', curPath)
					store.commit('logout')
					if (curPath === loginPath) {
						resolve(res.data)
					} else {
						uni.redirectTo({
							url: loginPath
						})
					}
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 1500
					})
				}
			},
			fail: function(err) {
				uni.showToast({
					title: '请稍后再试',
					icon: 'none'
				})
			}

		})
	})
}

Vue.prototype.dateFtt = function(fmt, date) {
	var o = {
		"M+": date.getMonth() + 1, //月份
		"d+": date.getDate(), //日
		"h+": date.getHours(), //小时
		"m+": date.getMinutes(), //分
		"s+": date.getSeconds(), //秒
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度
		"S": date.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k])
				.length)));
	return fmt;
}


App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()